package cn.minimelon.api.service.data.listener;

import cn.minimelon.api.service.data.factory.TopicRecFactory;
import lombok.extern.slf4j.Slf4j;
import org.noear.redisx.RedisClient;
import org.noear.redisx.plus.RedisBus;
import org.noear.solon.annotation.Component;
import org.noear.solon.annotation.Init;
import org.noear.solon.annotation.Inject;

@Slf4j
@Component
public class TopicRecListener {
    @Inject
    private RedisClient redisClient;

    @Inject
    private TopicRecFactory topicRecFactory;

    /**
     * 初始化订阅处理
     */
    @Init
    public void initSubscribe() {
        Thread thread = new Thread(() -> {
            String[] channels = {"model:deploy", "flowable"};
            RedisBus bus = redisClient.getBus();
            bus.subscribe((topic, message) -> topicRecFactory.getConsumer(topic).accept(message), channels);
        });
        thread.start();
    }
}
